/**
 * 
 */
package neptune.web.client.services;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.PopupPanel;

import neptune.web.client.WebInterface;
import neptune.web.client.view.ConsoleManager;
import neptune.web.client.view.components.MessageWidget;
import neptune.web.client.view.components.NodesManager;
import neptune.web.client.view.components.NodesManager.NodeLiveDataProvider;

/**
 * Fonisce le informazioni riguardanti i nodi virtuali residenti sulla macchina
 * fisica il cui identificativo � passato al costruttore di questa classe.
 * 
 * @author Roberto Bifulco [RobertoBifulco.it] [info@robertoBifulco.it]
 * 
 */
public class PMHostedNodesDataProvider implements NodeLiveDataProvider {

	private final String pmId;

	public PMHostedNodesDataProvider(String pmId) {
		this.pmId = pmId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * neptune.web.client.view.components.NodesManager.NodeLiveDataProvider#
	 * getNodeInfos(java.lang.String,
	 * neptune.web.client.view.components.NodesManager)
	 */
	public void getNodeInfos(String nodeId, NodesManager manager) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * neptune.web.client.view.components.NodesManager.NodeLiveDataProvider#
	 * getNodesInfos(neptune.web.client.view.components.NodesManager)
	 */
	public void getNodesInfos(final NodesManager manager) {
		//String message = "Loading physical machine '" + pmId+ "' hosted node infos...";
		String message = "Loading physical machine hosted node infos...";
		final PopupPanel popup = WebInterface.showMessageDialog(
				new MessageWidget(MessageWidget.LOADING, message), false);
		ConsoleManager.addConsoleMessage(message, false);

		ClusterService.Util.getInstance().getVMsHostedByPM(pmId,
				new AsyncCallback<String[][]>() {

					public void onSuccess(String[][] result) {
						popup.hide();
						String message = "Physical machine '" + pmId
								+ "' hosted node infos successfully loaded";
						ConsoleManager.addConsoleMessage(message, false);

						for (String[] vmData : result) {
							manager.addNode(vmData[0], vmData[1], vmData[2]);
						}
					}

					public void onFailure(Throwable caught) {
						popup.hide();
						String message = "Unable to load Physical machine '"
								+ pmId + "' hosted node infos. Cause: \n"
								+ caught.getMessage();
						WebInterface.addMessageToConsoleAndShowDialog(message, true);

					}
				});
	}

}
